package com.apexsoft.monitor.front.console.scheme.service;

import com.alibaba.fastjson.JSONObject;
import com.apexsoft.front.common.support.Result;
import com.apexsoft.monitor.front.console.scheme.dao.LinkDao;
import com.apexsoft.monitor.front.console.scheme.om.Link;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.pagination.Pagination;
import org.apache.commons.lang.StringUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Service("linkService")
public class LinkService {
    private static final Logger log = LogManager.getRootLogger();

    @Autowired
    @Qualifier("linkDao")
    private LinkDao linkDao = null;

    /**
    * 分页面查询数据
    * @param page
    * @param linkName
    * @return
    * @throws Exception
    */
    public JSONObject listByPage(Pagination page, String linkName) throws Exception {
        JSONObject result = new JSONObject();
        EntityWrapper<Link> entityWrapper = new EntityWrapper<>();
        if (StringUtils.isNotBlank(linkName)) {
            entityWrapper.like("link_name",linkName);
        }
        List<Link> list = linkDao.selectPage(page,entityWrapper);
            if(list != null){
                result.put(Result.CODE,Result.SC_0001);
                result.put(Result.NOTE,"查询成功");
                result.put(Result.TOTAL,list.size());
                result.put(Result.ROWS,list);
            }else{
                result.put(Result.CODE, Result.EC_1000);
                result.put(Result.NOTE, "查询失败");
            }
                return result;
            }

    /**
    * 新增链路
    * @param request
    * @return
    */
    public JSONObject add(HttpServletRequest request){
        JSONObject result = new JSONObject();
        String startNode = request.getParameter("startNode");
        String endNode = request.getParameter("endNode");
        String schemeCode = request.getParameter("schemeCode");
        String linkCode = request.getParameter("linkCode");
        if(
         StringUtils.isNotBlank(startNode)
            && StringUtils.isNotBlank(endNode)
            && StringUtils.isNotBlank(schemeCode)
            && StringUtils.isNotBlank(linkCode)
        ){
            Link link = new Link();
            link.setStartNode(startNode);
            link.setEndNode(endNode);
            link.setSchemeCode(schemeCode);
            link.setLinkCode(linkCode);
            try{
                long insertRst = linkDao.insert(link);
                if(insertRst > Result.EC_1000){
                    result.put(Result.CODE, Result.SC_0001);
                    result.put(Result.NOTE, "新增链路成功");
                }
            }catch (Exception e){
                log.error("新增链路失败："+e.getMessage(), e);
                result.put(Result.CODE, Result.EC_1000);
                result.put(Result.NOTE, "新增链路失败");
            }
        }else{
            result.put(Result.CODE, Result.EC_1000);
            result.put(Result.NOTE, "参数不正确");
        }
        return result;
    }

    /**
    * 更新链路
    * @param request
    * @return
    */
    public JSONObject update(HttpServletRequest request){
        JSONObject result = new JSONObject();
        String id = request.getParameter("id");
        String startNode = request.getParameter("startNode");
        String endNode = request.getParameter("endNode");
        String schemeCode = request.getParameter("schemeCode");
        String linkCode = request.getParameter("linkCode");
        if(
 StringUtils.isNotBlank(startNode)
            && StringUtils.isNotBlank(endNode)
            && StringUtils.isNotBlank(schemeCode)
            && StringUtils.isNotBlank(linkCode)
        ){
            Link link = new Link();
            link.setId(Integer.parseInt(id));
            link.setStartNode(startNode);
            link.setEndNode(endNode);
            link.setSchemeCode(schemeCode);
            link.setLinkCode(linkCode);
            try {
                int uptRst = linkDao.updateSelectiveById(link);
                if(uptRst > Result.EC_1000){
                    result.put(Result.CODE, Result.SC_0001);
                    result.put(Result.NOTE, "修改链路机成功");
                }
            }catch (Exception e){
                log.error("修改链路失败："+e.getMessage(), e);
                result.put(Result.CODE, Result.EC_1000);
                result.put(Result.NOTE, "修改链路失败");
            }
        }
        return result;
    }

    /**
    * 删除链路
    * @param id
    * @return
    */
    public JSONObject delete(String id){
        JSONObject result = new JSONObject();
        if(StringUtils.isNotBlank(id)){
            try {
                int delRst = linkDao.deleteById(id);
                if(delRst>Result.EC_1000){
                    result.put(Result.CODE, Result.SC_0001);
                    result.put(Result.NOTE, "删除链路成功");
                }
            }catch (Exception e){
                log.error("删除链路失败："+e.getMessage(), e);
                result.put(Result.CODE, Result.EC_1000);
                result.put(Result.NOTE, "删除链路失败");
            }
        }
        return result;
    }

    /**
     * 根据schemeCode,删除链路信息
     * @param schemeCode
     * @return
     */
    public JSONObject deleteByScheme(String schemeCode){
        JSONObject result = new JSONObject();
        if(StringUtils.isNotBlank(schemeCode)){
            try {
                int delRst = linkDao.deleteByScheme(schemeCode);
                if(delRst>Result.EC_1000){
                    result.put(Result.CODE, Result.SC_0001);
                    result.put(Result.NOTE, "删除链路成功");
                }
            }catch (Exception e){
                log.error("删除链路失败："+e.getMessage(), e);
                result.put(Result.CODE, Result.EC_1000);
                result.put(Result.NOTE, "删除链路失败");
            }
        }
        return result;
    }

    /**
    * 查询单条记录
    * @param id
    * @return
    */
    public Link getById(int id){
        return linkDao.selectById(id);
    }

    /**
     *
     * @param startNode
     * @return
     */
    public JSONObject selectByStartNode(String startNode){
        JSONObject result = new JSONObject();
        List<Link> list = linkDao.selectByStartNode(startNode);
        if(list != null){
            result.put(Result.CODE,Result.SC_0001);
            result.put(Result.NOTE,"查询成功");
            result.put(Result.TOTAL,list.size());
            result.put(Result.ROWS,list);
        }else{
            result.put(Result.CODE, Result.EC_1000);
            result.put(Result.NOTE, "查询失败");
        }
        return result;
    }

}
